﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Reminder
{
    public enum LocationType
    {
        Fixed,
        Mobile
    }

    public class Position
    {
        [XmlAttribute]
        public string Id { get; set; }
        [XmlAttribute]
        public Interfaces.PositionOriginator Type { get; set; }
    }

    [Serializable]
    public class Location
    {
        public string Name { get; set; }
        public LocationType Type { get; set; }

        [XmlIgnore]
        public Location Parent { get; set; }

        private Guid guid;
        public Guid Guid
        {
            get
            {
                if (guid == Guid.Empty)
                {
                    guid = Guid.NewGuid();
                }
                return guid;
            }
            set
            {
                guid = value;
            }
        }

        internal Guid loadedParentGuid;
        [XmlElement]
        public Guid ParentGuid
        {
            get
            {
                if (Parent != null)
                {
                    return Parent.Guid;
                }
                else
                {
                    //TODO: maybe it should return loadedParentGuid if available
                    return Guid.Empty;
                }
            }
            set
            {
                loadedParentGuid = value;
            }
        }

        /// <summary>
        /// Tells the serializer whether or not it has to serialize the ParentGuid property.
        /// 
        /// Doesn't seem to work in the Compact Framework. :)
        /// </summary>
        /// <remarks>
        /// The name of this method is recognised by the framework. It must not be altered!
        /// </remarks>
        public bool ShouldSerializeParentGuid()
        {
            return Parent != null;
        }

        //private List<string> positions;
        private List<Position> positions;
        [XmlArrayItem("Position")]
        //public List<string> Positions
        public List<Position> Positions
        {
            get
            {
                if (positions == null)
                {
                    //positions = new List<string>();
                    positions = new List<Position>();
                }
                return positions;
            }
            set
            {
                positions = value;
            }
        }

        public Location()
        {

        }

        public override string ToString()
        {
            return Name;
        }

        public override int GetHashCode()
        {
            return this.Guid.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            //return obj != null && obj == this || guid != Guid.Empty &&
            //    obj.GetType() == GetType() && ((Location)obj).guid == guid;

            if (obj == null)
                return false;

            if (obj == this)
                return true;

            if (guid == Guid.Empty)
                return false;

            if (obj.GetType() == GetType() && ((Location)obj).guid == guid)
                return true;

            return false;
        }
    }
}
